package entity.thread.bank;


public class BankTest {

    public static void main(String[] args) {
        Account account=new Account(150,"结婚基金");
        Thread t2= new Thread(new BankSys(account,100,"女人"));
        Thread t1= new Thread(new BankSys(account,60,"男人"));
        t1.start();
        t2.start();
    }



}

class Account{
    int money;
    String name;

    public Account(int money, String name) {
        this.money = money;
        this.name = name;
    }
}

class BankSys extends Thread {

    final Account account;

    int getMoney;//要取的钱

    int nowMoney;//现在手里的钱

    public BankSys(Account account, int getMoney,String name) {
        super(name);
        this.account = account;
        this.getMoney = getMoney;
    }

    @Override
    public void run() {

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        synchronized (account){
            if (account.money-getMoney<0){
                System.out.println(this.getName()+"取钱时余额不足");
                return;
            }
            //卡里余额的钱等于总额-取得的钱
            account.money-=getMoney;
            //手里的钱等于之前的钱+取得的钱
            nowMoney+=getMoney;

            System.out.println(this.getName()+"取钱"+getMoney+",剩余"+account.money);
        }

    }
}